package com.jiang.myfm.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;

import com.jiang.myfm.R;
import com.jiang.myfm.VideoActivity;
import com.jiang.myfm.adapters.Find3adaptercon;
import com.jiang.myfm.adapters.Find3adaptertit;
import com.jiang.myfm.entities.guobobean.Content;
import com.jiang.myfm.entities.guobobean.Data;
import com.jiang.myfm.entities.guobobean.Title;
import com.jiang.myfm.interfaces.Httpservice;
import com.jiang.myfm.newview.GredView;
import com.jiang.myfm.newview.NListview;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2016/12/2.
 */
public class Findfragment_3 extends Fragment implements Callback<Data> {
    private GredView gv;
    private NListview lv1,lv2;
    private ImageView iv1,iv2,iv3,iv4;
    private List<Title> listitle,listtitle1=new ArrayList<>();
    private  List<Content> contentlist,contentlist1=new ArrayList<>(),contentlist2;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.findfragment_3item,container,false);
        initView(view);
        return view;
    }

    private void initView(View view) {
  iv1= (ImageView) view.findViewById(R.id.iv1);
        iv2= (ImageView) view.findViewById(R.id.iv2);
        iv3= (ImageView) view.findViewById(R.id.iv3);
        iv4= (ImageView) view.findViewById(R.id.iv4);
        lv1= (NListview) view.findViewById(R.id.lv1);
        lv2= (NListview) view.findViewById(R.id.lv2);
        gv= (GredView) view.findViewById(R.id.gv);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Retrofit retrofit=new Retrofit.Builder().baseUrl("http://live.ximalaya.com/").addConverterFactory(GsonConverterFactory.create()).build();
        Httpservice service=retrofit.create(Httpservice.class);
        service.getMessage().enqueue(this);
    }

    @Override
    public void onResponse(Call<Data> call, Response<Data> response) {
       Data data= response.body();
      listitle=  data.getBean().getListitle();
        contentlist=data.getBean().getContentList();
        contentlist2=data.getBean().getTopRadios();
        for(int i=0;i<7;i++){
            listtitle1.add(listitle.get(i));
        }
        listitle.removeAll(listtitle1);
        Find3adaptertit adapter=new Find3adaptertit(getActivity(),listtitle1,listitle);
        gv.setAdapter(adapter);

        Find3adaptercon adapter1=new Find3adaptercon(getActivity(),contentlist);
        lv1.setAdapter(adapter1);
        for(int i=0;i<3;i++){
            contentlist1.add(contentlist2.get(i));
        }
        Find3adaptercon  adapter2=new Find3adaptercon(getActivity(),contentlist1);
        lv2.setAdapter(adapter2);
        lv2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(getActivity(), VideoActivity.class);
                String url=contentlist1.get(position).getPlayUrl().getUrl();
                intent.putExtra("url",url);
                startActivity(intent);
            }
        });
        lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(getActivity(), VideoActivity.class);
                String url=contentlist.get(position).getPlayUrl().getUrl();
                intent.putExtra("url",url);
                startActivity(intent);
            }
        });


     }

    @Override
    public void onFailure(Call<Data> call, Throwable t) {

    }
}
